﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using System.Collections;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using NHibernate.Transform;

namespace ZLCMS.Web.Wap.Shop
{
    public partial class UC_ShopNavBar : System.Web.UI.UserControl
    {
        public int CurrentCategory { get; set; }

        protected IList<IDictionary> Kids { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["navbar"] == "1")
            {
                Kids = GetKids();
                if (Kids.Count > 0)
                {
                    if (!string.IsNullOrEmpty(Request["kid"]))
                    {
                        try
                        {
                            CurrentCategory = Int32.Parse(Request["kid"]);
                        }
                        catch
                        {
                            CurrentCategory = (int)Kids[0]["id"];
                        }
                    }
                    else
                    {
                        CurrentCategory = (int)Kids[0]["id"];
                    }
                }
                else
                {
                    CurrentCategory = Int32.Parse(Request["categoryid"]);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Request["categoryid"]))
                {
                    CurrentCategory = Int32.Parse(Request["categoryid"]);
                }
            }
        }

        protected IList<IDictionary> GetKids()
        {
            var cacheKey = "kids_" + Request["categoryid"];

            //用cache缓存数据；
            var result = Cache[cacheKey] as IList<IDictionary>;
            if (result == null)
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {

                    result = session.CreateSQLQuery("select category_name name,id id from shop_category where is_show = '1' and parent_id=" + Int32.Parse(Request["categoryid"])).SetResultTransformer(Transformers.AliasToEntityMap).List<IDictionary>();

                    Cache.Insert(cacheKey
    , result, null
    , DateTime.Now.AddSeconds(600), TimeSpan.Zero);
                }
            }

            return result;
        }
    }
}